﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Win.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CtlMapLayers : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public CtlMapLayers()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        public List<dynamic> SelectDatas { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtlMapLayers_Load(object sender, EventArgs e)
        {
            dgrdvwLayers.ColumnHeaderMouseClick += ControlHelper.dataGridViewX_ColumnHeaderMouseClick;
        }

        private IMap _Map = null;

        /// <summary>
        /// 绑定地图对象
        /// </summary>
        /// <param name="hook">需要绑定的对象：AxMapControl.Object</param>
        /// <param name="tocObject">toc控件的object</param>
        public void OnCreate(object hook, object tocObject = null)
        {
            layersTree.OnCreate(hook, tocObject);
            
        }
        
        /// <summary>
        /// 初始化图层列表和图层树
        /// </summary>
        private void RefreshLayers()
        {
            if (_Map == null) return;
            IEnumLayer eulyr = _Map.get_Layers();
            ILayer lyr = null;
            while ((lyr = eulyr.Next()) != null)
            {
                if (!(lyr is IFeatureLayer)) continue;
                IFeatureLayer featlyr = lyr as IFeatureLayer;
                if (!featlyr.Valid) continue;
                if (featlyr.FeatureClass.FeatureType == esriFeatureType.esriFTSimple ||
                    featlyr.FeatureClass.FeatureType == esriFeatureType.esriFTAnnotation)
                {
                    string strFclsName = ((IDataset)featlyr.FeatureClass).Name;                    
                    int idx = dgrdvwLayers.Rows.Add();
                    dgrdvwLayers.Rows[idx].Cells[dgrdvwcolChk.Index].Value = featlyr.Visible;                    
                    dgrdvwLayers.Rows[idx].Cells[dgrdvwcolName.Index].Value = strFclsName;
                    dgrdvwLayers.Rows[idx].Cells[dgrdvwcolSource.Index].Value = ((IDataset)featlyr.FeatureClass).FullName.ToString();
                    //属性过滤条件默认显示该图层的DefinitionExpression
                    IFeatureLayerDefinition pFeatureLayerDefinition = featlyr as IFeatureLayerDefinition;
                    string definitionExpression = pFeatureLayerDefinition.DefinitionExpression;
                    dgrdvwLayers.Rows[idx].Cells[dgrdvwcolWhere.Index].Value = definitionExpression;
                    dgrdvwLayers.Rows[idx].Tag = featlyr;
                }
            }
        }


        
        /// <summary>
        /// 获取所有选中
        /// </summary>
        /// <returns></returns>
        private List<IFeatureClass> GetCheckLayers()
        {
            List<IFeatureClass> lyrs = new List<IFeatureClass>();
            if (sptbctlLayers.SelectedTab == sptbitmList)
            {
                foreach (DataGridViewRow r in dgrdvwLayers.Rows)
                {
                    if (!(bool)(r.Cells[dgrdvwcolChk.Index].Value)) continue;
                    if (r.Tag == null) continue;
                    lyrs.Add((r.Tag as IFeatureLayer).FeatureClass);
                }
            }
            else
            {

            }
            return lyrs;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgrdvwLayers_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (this.dgrdvwLayers.RowCount == 0) return;
            //if (e.ColumnIndex != 3) return;
            //DataGridViewRow dgvR = this.dgrdvwLayers.Rows[e.RowIndex];
            //if (dgvR.Tag != null && dgvR.Tag is IFeatureLayer)
            //{
            //    FrmQueryByAttribute frmSQL = new FrmQueryByAttribute();
            //    frmSQL.Application = m_Application;
            //    frmSQL.FeatureLayerExp = (dgvR.Tag as IFeatureLayer);
            //    frmSQL.WhereCause = (dgrdvwLayers.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            //    frmSQL.CmdOKClick = delegate(string sSQL)
            //    {
            //        dgrdvwLayers.Rows[e.RowIndex].Cells[3].Value = sSQL;
            //        dgrdvwLayers.RefreshEdit();
            //    };
            //    frmSQL.ShowDialog(this);
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgrdvwLayers_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            ////第一列复选框的值发生变化时，联动改变树节点的勾选情况
            //if (e.ColumnIndex == 0)
            //{
            //    //增加这句话，预防初始化时出错
            //    if ((dgrdvwLayers.Rows.Count == 0) || (e.RowIndex < 0)) return;
            //    if (dgrdvwLayers.Rows[e.RowIndex].Tag == null || !(dgrdvwLayers.Rows[e.RowIndex].Tag is IFeatureLayer)) return;

            //    IFeatureLayer pFeatLyr = dgrdvwLayers.Rows[e.RowIndex].Tag as IFeatureLayer;
            //    List<TreeNode> treeNodes = TreeHelper.GetAllYzTreeNode2(treeView1.Nodes[0]);
            //    foreach (TreeNode treeNode in treeNodes)
            //    {
            //        if (treeNode.Tag == null || !(treeNode.Tag is IFeatureLayer)) continue;
            //        IFeatureLayer pFeatLyr2 = treeNode.Tag as IFeatureLayer;
            //        if (pFeatLyr.Equals(pFeatLyr2))
            //        {
            //            treeNode.Checked = (bool)dgrdvwLayers.Rows[e.RowIndex].Cells[0].Value;
            //            TreeHelper.CheckChildrenBySelf(treeNode, treeNode.Checked);
            //            TreeHelper.SetParentNodeState(treeNode);
            //            return;
            //        }
            //    }

            //}
        }

    }
}
